package com.pay.download.processor;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.pay.entity.ZfbInLog;
import com.pay.entity.ZfbMember;
import com.pay.utils.FunctionUtils;

import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 切换成标准版
 * @author Administrator
 *
 */
public class StandardProcessor {
	
	/**
	 * 处理器
	 * @return
	 */
	public static Map<String,String> processor(String result,ZfbMember zfbMember){
		Map<String, String> map = new HashMap<String,String>();
		map.put("status", "success") ;
		if(StrUtil.isBlank(result)) {
			map.put("status", "error") ;
			return map ;
		}
		Document doc = Jsoup.parse(result);
		String title = doc.title();
		if(!"我的账单 - 支付宝".equals(title)) {
			//警告
			map.put("status", "warning") ;
			return map ; 
		}
		Elements supper = doc.select("a[seed=CR-AdvancedFilter]");
		if(supper == null || supper.size() < 1) {
			//警告
			map.put("status", "warning") ;
			return map ; 
		}
		if(supper.get(0).text().indexOf("高级筛选") == -1) {
			//警告
			map.put("status", "warning") ;
			return map ; 
		}
		
		Elements trElements = doc.select(".J-item");
		List<ZfbInLog> list = new ArrayList<ZfbInLog>(); //这边存放的全部都是到款记录
		for(int i=trElements.size() - 1;i>=0;i--) {
			Element trElement = trElements.get(i) ;
			
			String consumeBizNo = trElement.select(".J-tradeNo").attr("title");
			boolean exists = FunctionUtils.isBloomFilter(consumeBizNo);
			if(exists) {
				//存在的话就退出
				continue ; 
			}
			String flagcode = trElement.select(".consume-title").text();
			
			if(StrUtil.isBlank(flagcode)) {
				continue ;
			}
			String statusStr = trElement.select(".status .text-muted").get(0).text();
			if(!"交易成功".equals(statusStr)) {
				continue ;
			}
			String income = trElement.select(".amount-pay").text();
			if(StrUtil.isBlank(income)) {
				continue ;
			}
			if(income.indexOf("+") == -1) {
				continue ;
			}
			Pattern pattern = Pattern.compile("([0-9]{1,}[.]*[0-9]*)");
			Matcher matcher = pattern.matcher(income); //只取数字部分
			if(!matcher.find()) {
				continue; 
			}
			String name = trElement.select(".name .name").get(0).text();
			name = UnicodeUtil.toString(name) ; 
			income = matcher.group(1) ;
			
			pattern = Pattern.compile("b=(.+)");
			matcher = pattern.matcher(flagcode);
			if(matcher.find()) {
				//匹配成功
				flagcode = matcher.group(1) ;
			}else {
				continue ; 
			}
			
			ZfbInLog log = new ZfbInLog();
			log.setMemberId(zfbMember.getId());
			log.setMoney(new BigDecimal(income));
			log.setCreatetime(new Date());
			log.setTradeNo(consumeBizNo);
			log.setFlagcode(flagcode);
			log.setTransname(name);
			list.add(log) ;
		}
		String data = JSON.toJSONString(list);
		map.put("data", data) ;
		
		return map ; 
	}
}
